<?php

/**
 * 后台文章模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: article.php 47 2011-07-18 05:29:51Z zwt007 $
 * http://www.t-mac.org；
 */
class articleAction extends Action
{

    private $tmp_model;
    private $archives_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('article');
        $this->archives_model = Tmac::model('archives');

        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin,tb_user');
    }

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {
        $uid = intval($_SESSION['admin_uid']);
        $cat_id = intval($this->getParam('cat_id'));
        $channelid = intval($this->getParam('channelid'));
        //TODO  取出所有资讯
        $rs = $this->tmp_model->getArticleList($channelid, $uid, '', $cat_id);

        $this->assign('channelid', $channelid);
        $this->V('article');
    }

    /**
     * 新增/修改文章页面
     */
    public function add()
    {
        $aid = intval($this->getParam('aid'));
        $channeltype = 1;
        $cat_id = intval($this->getParam('cat_id'));
        $uid = intval($_SESSION['admin_uid']);
        if ($aid > 0) {
            //修改的情况下判断权限 
            $this->tmp_model->checkUserArticle($aid, $uid);
            $editinfo = $this->tmp_model->getArticleInfo($aid);
        } else {
            $editinfo = array('title' => '', 'author' => '', 'order' => '', 'content' => '', 'cat_id' => $cat_id, 'article_id' => '', 'url' => '', 'keywords' => '', 'description' => '', 'show' => 0);
        }
        $show_array = Tmac::config('channel.show');
        $show_option = Utility::RadioButton($show_array, 'show', $editinfo['show']);
        //取文章栏目        
        //$treers = $this->tmp_model->article_cat_tree(0, 0, $editinfo['cat_id'], $channeltype);        
        $treers = $this->M('category')->getCategoryTreeList(0, $uid, $editinfo['cat_id']);

        //初始化一下    默认state_radio
        $editinfo['author'] = !empty($editinfo['author']) ? $editinfo['author'] : '默认管理员';
        $editinfo['order'] = !empty($editinfo['orderby']) ? $editinfo['orderby'] : '0';

        $this->assign('show_option', $show_option);
        $this->assign('editinfo', $editinfo);
        $this->assign('treers', $treers);
        //TODO　载入资讯类别添加表单
        $this->V('article');
    }

    /**
     * ajax根据省pid 取下级city list
     */
    public function getCity()
    {
        $pid = (int) $_POST['pid'];
        $type = !empty($_POST['type']) ? $_POST['type'] : 'yes';

        $city_ary = $this->tmp_model->getCity('c', $pid);
        //把缩写和名字组合在一起
        $city_array = array();
        foreach ($city_ary AS $k => $v) {
            $cityid = $v['cityid'];
            if ($type == 'yes') {
                $cityname = $v['suoxie'] . ' ' . $v['cityname'];
            } else {
                $cityname = $v['cityname'];
            }
            $city_array[] = array('cityid' => '' . $cityid . '', 'cityname' => '' . $cityname . '');
        }
        $rs = json_encode($city_array);
        exit($rs);
    }

    /**
     * 新增/修改栏目页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 1) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $aid = intval($this->getParam('aid'));
        $cat_id = intval($this->getParam('cat_id'));
        $title = trim($this->H($this->getParam('title')));
        $author = trim($this->H($this->getParam('author')));
        $order = intval($this->getParam('order'));
        $url = $this->H($this->getParam('thumb'));
        $content = $this->getParam('content');
        $keywords = $this->H($this->getParam('keywords'));
        $description = $this->H($this->getParam('description'));
        $channelid = 1;
        $uid = intval($_SESSION['admin_uid']);
        $show = intval($this->getParam('show'));

        if (!$this->archives_model->checkChannel($cat_id, $channelid)) {
            $this->redirect("你所选择的栏目与当前模型不相符！");
            exit();
        }

        //判断是否为空
        if (empty($title)) {
            $this->redirect('标题不能力空');
            exit;
        }

        if (empty($content)) {
            $this->redirect('内容不能力空');
            exit;
        }

        $info = array(
            'cat_id' => $cat_id,
            'channel' => $channelid,
            'uid' => $uid,
            'title' => $title,
            'author' => $author,
            'orderby' => $order,
            'keywords' => $keywords,
            'description' => $description,
            'url' => $url,
            'time' => time(),
            'show' => $show
        );

        $type_url = PHP_SELF . '?m=archives.arclist&channelid=' . $channelid . '&cat_id=' . $cat_id;

        if ($aid > 0) {
            //修改的情况下判断权限 
            $this->tmp_model->checkUserArticle($aid, $uid);
            //不改uid
            unset($info['uid']);
            $article_page = HttpResponse::getCookie($cat_id . '_page');
            //update save article
            $where = 'article_id = ' . $aid . '';
            $rs = $this->tmp_model->addArticle('article', $info, 'UPDATE', $where);
            if ($rs) {
                $info_addonarticle = array('article_id' => $aid, 'cat_id' => $cat_id, 'content' => $content);
                $rs = $this->tmp_model->addArticle('addon', $info_addonarticle, 'UPDATE', $where);
                if ($rs) {
                    $this->redirect('修改资讯文章成功', $type_url . '&page= ' . $article_page . '');
                } else {
                    $this->redirect('修改资讯文章从表失败，请联系技术支持检查原因！');
                }
            } else {
                $this->redirect('修改资讯文章失败');
            }
        } else {
            //insert save article_class
            $article_id = $this->tmp_model->addArticleReturn($info);
            if ($article_id) {
                $info_addonarticle = array('article_id' => $article_id, 'cat_id' => $cat_id, 'content' => $content);
                $rs = $this->tmp_model->addArticle('addon', $info_addonarticle, 'INSERT');
                if ($rs) {
                    $this->redirect('添加资讯文章成功', $type_url);
                } else {
                    $this->redirect('添加资讯文章从表失败，请联系技术支持检查原因！');
                }
            } else {
                $this->redirect('添加资讯文章主表失败，请联系技术支持检查原因！');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $aid = intval($this->getParam('aid'));
        $id_a = $this->getParam('id_a');
        $uid = intval($_SESSION['admin_uid']);

        if (is_array($id_a)) {
            $id = implode(',', $id_a);
            foreach ($id_a AS $k => $v) {
                $this->tmp_model->checkUserArticle($v, $uid);
            }
        } elseif (!empty($aid)) {
            $id = $aid;
            $this->tmp_model->checkUserArticle($aid, $uid);
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->delArticle($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $this->redirect('删除资讯成功');
        } else {
            $this->redirect('删除资讯失败，请重试！');
        }
    }

}